//
//  StatuesModel.m
//  微博
//
//  Created by zxy on 2018/12/20.
//  Copyright © 2018 zxy. All rights reserved.
//

#import "StatuesModel.h"
@interface StatuesModel()
@property(nonatomic,strong)NSDateFormatter *fmt;
@end
#define wbUrl @"https://api.weibo.com/2/statuses/home_timeline.json?access_token=2.00N5a_ZF_YI9wC326ccf9f3c2kkNtC"
@implementation UserModel
+(NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{
             @"userId":@"id"
             };
}
@end

@implementation StatuesModel

+(NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{
             @"wbId":@"id"
             };
}

-(NSDateFormatter *)fmt{
    if(!_fmt){
        _fmt = [NSDateFormatter new];
         _fmt.dateFormat = @"EEE MMM d HH:mm:ss Z yyyy";
    }
    return _fmt;
}

-(NSArray *)big_picUrls{
    NSMutableArray *bigArray = [NSMutableArray array];
    for (NSDictionary *dic in self.pic_urls) {
        NSString *url = dic[@"thumbnail_pic"];
        NSString *bigUrl = [url stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"large"];
        [bigArray addObject:bigUrl];
    }
    return bigArray;
}

/*处理微博来源*/
//这里使用set方法，如果使用get方法会多次调用，影响性能
-(void)setSource:(NSString *)source{
    if(!_source.length){
        _source = @"";
        return;
    }
    //开始截取字符串位置
    NSUInteger startLoation = [source rangeOfString:@">"].location + 1;
    //结束截取字符串位置
    NSUInteger endLoation = [source rangeOfString:@"<" options:NSBackwardsSearch].location;
    //获取截取字符串长度
    NSUInteger length = endLoation - startLoation;
    NSString *soureStr = [source substringWithRange:NSMakeRange(startLoation, length)];
    _source = [NSString stringWithFormat:@"来自%@",soureStr];
}

/*处理微博日期*/
-(void)setCreated_at:(NSString *)created_at{
    /*
     刚刚(一分钟)
     X分钟前(一小时内)
     X小时前(当天)
     昨天 HH:mm(昨天)
     MM-dd HH:mm(一年内)
     yyyy-MM-dd HH:mm(更早期)
     */
    
    //格林时间
    NSDate *date = [self.fmt dateFromString:created_at];
    
    //获取当前时间
    NSDate *currentDate = [NSDate date];
    //创建日历对象
    NSCalendar *calendar = [NSCalendar sharedCalendar];
    NSString *create_At = @"";
    
    //判断是否是今天
    if([calendar isDateInToday:date]){
        //计算格林时间与当前时间之间的差距(s/秒)
        NSInteger seconds = [currentDate timeIntervalSinceDate:date];
        if(seconds < 60){
            create_At = @"刚刚";
        }else if(seconds < 60 * 60){
            create_At = [NSString stringWithFormat:@"%ld分钟前",seconds/60];
        }else{
            create_At = [NSString stringWithFormat:@"%ld小时前",seconds/(60*60)];
        }
    }
    _created_at = create_At;
}

//计算cell高度
-(CGFloat)cellHeight{
    if(_cellHeight > 0)return _cellHeight;
    _cellHeight = 60;
    //计算中间内容高度
    CGFloat contentHeight = [self.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 *picMargin, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.height;
    _cellHeight += contentHeight;
    _cellHeight += 10;//间距
    if(self.pic_urls.count > 0){//有配图视图
         self.picBgViewFrame = CGRectMake(picMargin, _cellHeight, self.calcPicSize.width, self.calcPicSize.height);
        _cellHeight += 10;//间距
        _cellHeight += self.calcPicSize.height;
    }
    _cellHeight += 50;
    return _cellHeight;
}

-(CGSize)calcPicSize{
    if(_calcPicSize.height > 0)return _calcPicSize;
    NSInteger picCount = self.pic_urls.count;
    if(picCount == 0){
        _calcPicSize = CGSizeZero;
        //更改配图视图内容高度
        return _calcPicSize;
    };
    if(picCount == 1){//1张图片返回图片尺寸
        //获取url，通过url获取缓存图片，来拿到图片尺寸
        NSString *url = self.pic_urls.firstObject[@"thumbnail_pic"];
        NSString* key = [[SDWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:url]];
        UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
        //因为配图视图是距离顶部内容为0，所以还应该加上下间距
        _calcPicSize  = CGSizeMake(image.size.width, image.size.height);
        return _calcPicSize;
    }
    else if(picCount == 4){//田字格
        CGFloat width = picWidth*2 + picMargin;
        _calcPicSize = CGSizeMake(width,width);
        return _calcPicSize;
    }else{//2,3,5,6,7,8,9
        //获取行数
        NSInteger rowCount = (picCount -1) / maxColum + 1;
        CGFloat width = maxColum * picWidth + (maxColum - 1)*picMargin;
        CGFloat height = rowCount *picWidth + rowCount*picMargin;
        _calcPicSize = CGSizeMake(width, height);
        return _calcPicSize;
    }
}


//获取首页用户数据
+(void)getWbDataResult:(void(^)(NSArray *modelArr,NSError *error))complate{
    NSMutableArray *models = [NSMutableArray array];
    [PPNetworkHelper GET:wbUrl parameters:nil success:^(id responseObject) {
      NSArray *modelArray = [StatuesModel mj_objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
        [models addObjectsFromArray:modelArray];
        //缓存缓存回调
        complate(models,nil);
    } failure:^(NSError *error) {
        complate(nil,error);
    }];
}

@end
